{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# ROC曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "digits = datasets.load_digits()\n",
    "X = digits.data\n",
    "y = digits.target.copy()\n",
    "\n",
    "# 把数据变为极度偏斜的数据\n",
    "# 把手写数字分为9和非9两大类， 重点关注的是分类为9的数字\n",
    "y[digits.target==9] = 1\n",
    "y[digits.target!=9] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection._split import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model.logistic import LogisticRegression\n",
    "\n",
    "log_reg = LogisticRegression()\n",
    "log_reg.fit(X_train, y_train)\n",
    "decision_scores = log_reg.decision_function(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from playML.metrics import FPR, TPR\n",
    "\n",
    "fprs = []\n",
    "tprs = []\n",
    "thresholds = np.arange(np.min(decision_scores), np.max(decision_scores), 0.1)\n",
    "for threshold in thresholds:\n",
    "    y_predict = np.array(decision_scores >= threshold, dtype=int)\n",
    "    fprs.append(FPR(y_test, y_predict))\n",
    "    tprs.append(TPR(y_test, y_predict))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 手工绘制ROC曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA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6HIdJosqYO11I6zeNyWyfY25Pq+dm5vXdLKxGVb7Pi4BFEXFLRKyNiCVdq64eVcZ8OXBBRGymdVudD3antMbs77/3/VbbvY808UTEBcAAcGbTtdQpIqYBnwPe23Ap3Tad1iGks2jNBm+OiFMy85lGq6rXcuCrmflPEfGHtG6yeXJm7m66sMlqqs0UevE5DlXGTEScA3wCWJqZv+tSbXXZ15gPB04GfhgRD9M69jo4yRebq3yfNwODmfl8Zj4EPEArJCarKmO+EFgFkJm3AgfTukfQVFXp3/tYTLVQ6MXnOOxzzBFxKvAlWoEw2Y8zwz7GnJnbMnNWZs7PzPm01lGWZuZQM+WOiyo/29+nNUsgImbROpy0sZtFjrMqY34UOBsgIl5DKxSm8jN7B4F3t89COgPYlpmPj+cHTKnDR9mDz3GoOObPADOB77TX1B/NzKWNFT1GFcc8pVQc8xrgrRGxAdgFfDQzJ+0suOKYLwH+LSI+TGvR+b2T+Ze8iLiWVrDPaq+TfBI4CCAzr6G1bnIeMAxsB9437jVM4r8/SdI4m2qHjyRJY2AoSJIKQ0GSVBgKkqTCUJAkFYaCVFFE7IqIOzu+5kfEWRGxrb19X0R8st23s/2nEfHZpuuXqphS1ylINduRma/vbGjfev3Hmfn2iDgMuDMi/qu9+4X2Q4A7IuJ7mXlLd0uW9o8zBWmcZOZvgduBE0e07wDuZJxvXCbVwVCQqjuk49DR90bujIijaN1naf2I9lfQugfRzd0pUzpwHj6SqnvJ4aO2P4qIO4DdwJXtWzGc1W6/i1Yg/HNmPtHFWqUDYihIY/fjzHz7ntojYgGwNiJWZead3S5O2h8ePpJq1r6N9ZXAx5quRdoXQ0HqjmuAN7XPVpImLO+SKkkqnClIkgpDQZJUGAqSpMJQkCQVhoIkqTAUJEmFoSBJKgwFSVLxf8x5KBLj3pd/AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1092e13c8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(fprs, tprs)\n",
    "plt.xlabel('FPR')\n",
    "plt.ylabel('TPR', rotation=0)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## scikit-learn中的ROC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4xLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvAOZPmwAAEJ1JREFUeJzt3X+s3XV9x/HniyLqJqjYa4L9YWssiVfdgN0AxkxZcKYQbZe4OXDEaZjd3DDLNGY4FzD4D87pphkbViX+SBDRROliDX84DMbYjhIKs2WYDhBaWaiCmAwR0ff+OKcfj5fee0/b+z2n957nI7nJ+X6/H855f3puefXz+Xx/pKqQJAnghHEXIEk6fhgKkqTGUJAkNYaCJKkxFCRJjaEgSWoMBUlSYyhIkhpDQZLUnDjuAo7UypUra926deMuQ5KWlNtvv/2HVTW1ULslFwrr1q1j165d4y5DkpaUJN8fpp3TR5KkxlCQJDWGgiSpMRQkSY2hIElqOguFJNcleTjJd+c4niQfT7IvyV1JzuqqFknScLocKXwG2DjP8QuADf2fLcC/dViLJGkInV2nUFW3Jlk3T5PNwOeq9zzQHUmel+S0qnqoq5o0nOt3PsBNuw+MuwxJs0y/6BSufOPLO/2Mca4prAIeHNje39/3NEm2JNmVZNfBgwdHUtwku2n3AfY+9JNxlyFpDJbEFc1VtRXYCjAzM1NjLmciTJ92Cl/881eNuwxJIzbOkcIBYM3A9ur+PknSmIwzFLYBb+2fhXQu8JjrCZI0Xp1NHyX5AnAesDLJfuBK4BkAVXUtsB24ENgHPA68vatadGSLx3sf+gnTp53ScUWSjkddnn108QLHC/irrj5fv+7Q4vEw/7OfPu0UNp9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Yfp8KXAjQFV9B3gWvXsELVdD/X0/WssxFCbxOQ4L9jnJmcAn6AXCUp9nhgX6XFWPVdXKqlpXVevoraNsqqpd4yl3UQzzu/1VeqMEkqykN5107yiLXGTD9PkB4HyAJC+jFwrL+bm924C39s9COhd4rKoeWqw3X3bTRzWBz3EYss8fBp4DfKm/pv5AVW0aW9HHaMg+LytD9vlm4PVJ9gK/AN5bVUt2FDxkn98DfDLJ39BbdH7bUv5HXpIv0Av2lf11kiuBZwBU1bX01k0uBPYBjwNvX9TPX8J/dpKkRbYcp48kSUfJUJAkNYaCJKkxFCRJjaEgSWoMBWlISX6RZPfAz7ok5yV5rL99d5Ir+20H9/93kn8cd/3SMJbddQpSh35aVWcM7ujfdv1bVfWGJL8J7E7y7/3Dh/Y/G7gjyVeq6tujLVk6Mo4UpEVSVf8H3A68dNb+nwK7WcSblkldMRSk4T17YOroK7MPJnkBvXss7Zm1//n07j9062jKlI6e00fS8J42fdT3u0nuAH4JXN2/DcN5/f130guEf66q/x1hrdJRMRSkY/etqnrDXPuTrAd2JLmxqnaPujjpSDh9JHWsfwvrq4G/HXct0kIMBWk0rgVe0z9bSTpueZdUSVLjSEGS1BgKkqTGUJAkNYaCJKkxFCRJjaEgSWoMBUlSYyhIkpr/B7gz4RSag/ucAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1099a5128>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.metrics.ranking import  roc_curve\n",
    "\n",
    "fprs, tprs, thresholds = roc_curve(y_test, decision_scores)\n",
    "plt.plot(fprs, tprs)\n",
    "plt.xlabel('FPR')\n",
    "plt.ylabel('TPR')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.98304526748971188"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算（衡量）曲线下面的面积\n",
    "from sklearn.metrics.ranking import roc_auc_score\n",
    "\n",
    "roc_auc_score(y_test, decision_scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
